package cn.signalpha.modules.shop.service;

import cn.signalpha.core.tool.jackson.JsonUtil;
import cn.signalpha.modules.shop.entity.GoodsSnapshot;
import cn.signalpha.modules.shop.mapper.GoodsSnapshotMapper;
import cn.signalpha.modules.shop.vo.GoodsVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
public class GoodsSnapshotService extends ServiceImpl<GoodsSnapshotMapper, GoodsSnapshot> {

	/**
	 * 保存商品快照
	 */
	public void saveGoodsSnapshot(GoodsVO goodsVO, Long userId, Integer type) {
		GoodsSnapshot goodsSnapshot = new GoodsSnapshot();
		goodsSnapshot.setGoodsId(goodsVO.getId());
		goodsSnapshot.setDetail(JsonUtil.toJson(goodsVO));
		goodsSnapshot.setType(type);
		goodsSnapshot.setCreateUserId(userId);
		goodsSnapshot.setCreateTime(new Date());
		save(goodsSnapshot);
	}

	/**
	 * 根据商品删除快照
	 */
	public void removeByGoodsId(Long goodsId) {
		baseMapper.delete(Wrappers.<GoodsSnapshot>lambdaQuery().eq(GoodsSnapshot::getGoodsId, goodsId));
	}
}

